/*
 * @Author: ZhaoZhiqi
 * @Date: 2023-11-13 18:15:38
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2024-02-25 15:04:02
 * @Description: 
 * @FilePath: \labtalk-admin\src\api\upload.js
 */
import axios from 'axios';
import { notifications } from '@mantine/notifications';

const service = axios.create({
  baseURL: `${import.meta.env.VITE_APP_HTTPURL}`,
  withCredentials: true,
  headers: {
    'content-type': 'multipart/form-data',
  },
  timeout: 60000, // 请求超时时间
});

// 响应拦截
service.interceptors.response.use((response) => {
  const res = response.data;
  if (res.status !== 200) {
    notifications.show({
      title: '操作失败',
      message: res.desc,
      autoClose: 1500,
      position: 'top-right',
      color: 'red',
    });
  }
  return res;
});

const upload = (url, data) => {
  return new Promise((resolve, reject) => {
    service({
      method: 'POST',
      url: `${url}`,
      data,
      headers: {
        'Authorization': localStorage.getItem("labtalk_token")
      },
    })
      .then((res) => {
        if (res.status === 200) {
          resolve(res);
        } else {
          reject(res);
        }
      })
      .catch((err) => {
        reject(err);
      });
  });
};

export default upload;
